package com.lxm.shownews.channel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lxm.shownews.bean.ImageUrl;
import com.lxm.shownews.bean.News;
import com.lxm.shownews.channel.NewsType;
import com.lxm.shownews.channel.OnRecyclerViewListener;

import java.util.List;

/**
 * Created by lxm on 2016/2/24.
 */
public abstract class NewsBaseAdapter extends RecyclerView.Adapter<NewsBaseAdapter.BaseVH> {
    //    public BaseVH baseVH;
    protected List<News> newsList;
    protected Context context;

    protected OnRecyclerViewListener mRecyclerClickListener = null;

    public void setmOnRecyclerClickListener(OnRecyclerViewListener mRecyclerClickListener) {
        this.mRecyclerClickListener = mRecyclerClickListener;
    }

    public static class BaseVH extends RecyclerView.ViewHolder {
        public BaseVH(View itemView) {
            super(itemView);
        }
    }

    public NewsBaseAdapter(List<News> newsList) {
        this.newsList = newsList;
    }

    public abstract NoneImgVH onCreateNoneImgVH(ViewGroup parent, int viewType);

    public abstract void onBindNoneImgVH(BaseVH baseVH, int position);

    public abstract OneImgVH onCreateOneImgVH(ViewGroup parent, int viewType);

    public abstract void onBindOneImgVH(BaseVH baseVH, int position);

    public abstract DoubleImgVH onCreateDoubleImgVH(ViewGroup parent, int viewType);

    public abstract void onBindDoubleImgVH(BaseVH baseVH, int position);


    public abstract TreeImgVH onCreateTreeImgVH(ViewGroup parent, int viewType);

    public abstract void onBindTreeImgVH(BaseVH baseVH, int position);

    public abstract MoreImgVH onCreateMoreImgVH(ViewGroup parent, int viewType);

    public abstract void onBindMoreImgVH(BaseVH baseVH, int position);

    @Override
    public BaseVH onCreateViewHolder(ViewGroup parent, int viewType) {
        NewsType type = NewsType.getType(viewType);
        context = parent.getContext();
        switch (type) {
            case NoneImg:
                return onCreateNoneImgVH(parent, viewType);
            case OneImg:
                return onCreateOneImgVH(parent, viewType);
            case DoubleImg:
                return onCreateDoubleImgVH(parent, viewType);
            case TreeImg:
                return onCreateTreeImgVH(parent, viewType);
            case MoreImg:
                return onCreateMoreImgVH(parent, viewType);
            default:
                return onCreateNoneImgVH(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(BaseVH holder, int position) {
        int itemViewType = getItemViewType(position);
        NewsType type = NewsType.getType(itemViewType);
        switch (type) {
            case NoneImg:
                onBindNoneImgVH(holder, position);
                break;
            case OneImg:
                onBindOneImgVH(holder, position);
                break;
            case DoubleImg:
                onBindDoubleImgVH(holder, position);
                break;
            case TreeImg:
                onBindTreeImgVH(holder, position);
                break;
            case MoreImg:
                onBindMoreImgVH(holder, position);
                break;
        }
    }


    @Override
    public int getItemCount() {
        return newsList.size();
    }

    @Override
    public int getItemViewType(int position) {
        List<ImageUrl> imageurls = newsList.get(position).getImageurls();
        if (null == imageurls) {
            return NewsType.getValue(NewsType.NoneImg);
        }
        int size = imageurls.size();
        if (size == 0) {
            return NewsType.getValue(NewsType.NoneImg);
        } else if (size == 1) {
            return NewsType.getValue(NewsType.OneImg);
        } else if (size == 2) {
            return NewsType.getValue(NewsType.DoubleImg);
        } else if (size >= 3 && size <= 9) {
            return NewsType.getValue(NewsType.TreeImg);
        } else {
            return NewsType.getValue(NewsType.MoreImg);
        }
    }

    public void addMore(List<News> moreList) {
        Log.i("---addMore--newList--", newsList.toString());
        Log.i("---addMore--moreList--", moreList.toString());
        boolean b = newsList.containsAll(moreList);
        Log.i("--------addMore-------", "---" + b);
        if (b) return;
        newsList.addAll(moreList);
        this.notifyDataSetChanged();
    }

}
